﻿using IBLL;
using IDAL;
using Models;
using Models.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
    public class ProductInfoBLL : IProductInfoBLL
    {
        private ICategoryDAL _categoryDAL;
        private IPurchaseDAL _purchaseDAL;
        private IProductInfoDAL _productInfoDAL;

        public ProductInfoBLL(
            ICategoryDAL categoryDAL
            , IProductInfoDAL productInfoDAL
            , IPurchaseDAL purchaseDAL
            )
        {
            _categoryDAL = categoryDAL;
            _purchaseDAL = purchaseDAL;
            _productInfoDAL = productInfoDAL;
        }

        #region 获取商品表的方法 (GetProductInfos)
        /// <summary>
        /// 获取商品表的方法
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="departmentName"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<GetProductInfoDTO> GetProductInfo(int page, int limit, string productName,string categorylD, out int count)
        {
            var data = from p in _productInfoDAL.GetEntities().Where(d => d.IsDelete == false)
                       join c in _categoryDAL.GetEntities().Where(u => u.IsDelete == false)
                       on p.CategorylD equals c.Id
                       into PCtemp
                       from pc in PCtemp.DefaultIfEmpty()
                       select new GetProductInfoDTO
                       {
                           Id = p.Id,
                           ProductName = p.ProductName,
                           ProductPrice = p.ProductPrice,
                           ProductNum = p.ProductNum,
                           Imgage = p.Imgage,
                           CategorylD = pc.CategoryName,
                           CreateTime = p.CreateTime
                       };

            if (!string.IsNullOrWhiteSpace(productName))
            {
                //条件精准查询
                data = data.Where(d => d.ProductName == productName);
            }
            if (!string.IsNullOrWhiteSpace(categorylD))
            {
                //条件精准查询
                data = data.Where(d => d.CategorylD == categorylD);
            }
            //计算数据总数
            count = data.Count();
            //分页(降序)
            var listpage = data.OrderByDescending(u => u.CreateTime).Skip(limit * (page - 1)).Take(limit).ToList();

            return listpage;
        }
        #endregion

        #region 创建商品 (CreateProductInfo)
        /// <summary>
        /// 创建商品
        /// </summary>
        /// <param name="prod">用户实体</param>
        /// <param name="msg">返回消息</param>
        /// <returns></returns>
        public bool CreateProductInfo(ProductInfo prod, out string msg)
        {
            if (string.IsNullOrWhiteSpace(prod.ProductName))
            {
                msg = "商品名称不能为空";
                return false;
            }
            //验证商品是否存在
            if (_productInfoDAL.GetEntities().FirstOrDefault(it => it.ProductName == prod.ProductName) != null)
            {
                msg = "商品已经存在";
                return false;
            }
            prod.Id = Guid.NewGuid().ToString();//用户id
            prod.CreateTime = DateTime.Now;//设置时间
            bool IsSuccess = _productInfoDAL.CreateEntity(prod);//调用方法
            msg = IsSuccess ? $"添加{prod.ProductName}成功" : "添加用户失败";
            
            return IsSuccess;
        }
        #endregion

        #region 删除的方法 (DeleteProductInfo)
        /// <summary>
        /// 删除的方法
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteProductInfo(string id)
        {
            ProductInfo prod = _productInfoDAL.GetEntityById(id);
            if (prod == null)
            {
                return false;
            }
            prod.IsDelete = true;
            prod.DeleteTime = DateTime.Now;
            _productInfoDAL.UpdateEntity(prod);
            return true;
        }
        #endregion

        #region 批量删除的方法 (DeleteListProductInfo)
        /// <summary>
        /// 批量删除的方法
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteListProductInfo(List<string> ids)
        {
            List<ProductInfo> prodList = _productInfoDAL.GetEntities().Where(it => ids.Contains(it.Id)).ToList();
            foreach (var item in ids)
            {
                ProductInfo prod = _productInfoDAL.GetEntityById(item);
                if (prodList == null)
                {
                    return false;
                }
                prod.IsDelete = true;
                prod.DeleteTime = DateTime.Now;

                _productInfoDAL.UpdateEntity(prod);
            }
            return true;
        }
        #endregion

        #region 修改的方法 (UpdateProductInfo)
        /// <summary>
        /// 修改用户的方法
        /// </summary>
        /// <param name="prod"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool UpdateProductInfo(ProductInfo prod, out string msg)
        {
            if (string.IsNullOrWhiteSpace(prod.ProductName))
            {
                msg = $"商品名称不能为空";
                return false;
            }
            ProductInfo entity = _productInfoDAL.GetEntityById(prod.Id);
            if (entity.Id == null)
            {
                msg = "ID不存在";
                return false;
            }
            //判断重复
            if(entity.ProductName!= prod.ProductName)
            {
                var data = _productInfoDAL.GetEntities().FirstOrDefault(it => it.ProductName == prod.ProductName);
                if (data != null)
                {
                    msg = "商品名已经被占用";
                    return false;
                }
            } 

            entity.ProductName = prod.ProductName;
            entity.ProductPrice = prod.ProductPrice;
            entity.Imgage = prod.Imgage;
            entity.ProductNum = prod.ProductNum;
            entity.CategorylD = prod.CategorylD;

            bool IsSuccess = _productInfoDAL.UpdateEntity(entity);//调用方法

            msg = IsSuccess ? $"修改成功" : "修改失败";

            return IsSuccess;
        }
        #endregion

        #region 根据ID获取数据返回赋值 (GetProductInfoById)
        /// <summary>
        /// 根据ID获取数据返回赋值
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ProductInfo GetProductInfoById(string id)
        {
            return _productInfoDAL.GetEntityById(id);
        }
        #endregion

        #region 获取数据库列表赋值下拉框 (GetSelectOption)
        /// <summary>
        /// 获取数据库列表赋值下拉框
        /// </summary>
        /// <returns></returns>
        public object GetSelectOption()
        {
            var categoryData = _categoryDAL.GetEntities().Where(it => it.IsDelete == false).Select(d => new
            {
                value = d.Id,
                title = d.CategoryName,
            });


            return new
            {
                categoryData = categoryData,
            };
        }
        #endregion
    }
}
